package com.scsj.supermarket.view.activity.settingmodel;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.scsj.supermarket.R;
import com.scsj.supermarket.base.AlterLoginPwdBase;
import com.scsj.supermarket.bean.BaseBean;
import com.scsj.supermarket.config.RxSPConfig;
import com.scsj.supermarket.presenter.AlterLoginPwdPresenter;
import com.scsj.supermarket.utils.Logger;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.vondear.rxtools.RxSPTool;


import es.dmoral.toasty.Toasty;
import okhttp3.RequestBody;

/**
 * 登录密码重置
 */

public class ChangeLoginPasswordActivity extends BaseActivity implements View.OnClickListener, AlterLoginPwdBase.IView {
    /**
     * 请输入当前密码
     */
    private EditText mEtOldPassword;
    /**  */
    private EditText mEtNewPassword;
    /**  */
    private EditText mEtAgainNewPassword;
    /**
     * 确定
     */
    private Button mBtnEnsure;
    /**
     * 忘记登录密码？
     */
    private TextView mForgetPswTv;
    private AlterLoginPwdPresenter alterLoginPwdPresenter;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_change_login_password);
//        ScreenAdapterTools.getInstance().loadView((ViewGroup) getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
        initToolbar();
        setToolbarTitle("修改登录密码");
        hasBack(true);
        alterLoginPwdPresenter = new AlterLoginPwdPresenter(this);
    }

    @Override
    protected void initView() {

        mEtOldPassword = (EditText) findViewById(R.id.et_old_password);
        mEtNewPassword = (EditText) findViewById(R.id.et_new_password);
        mEtAgainNewPassword = (EditText) findViewById(R.id.et_again_new_password);
        mBtnEnsure = (Button) findViewById(R.id.btn_ensure);
        mForgetPswTv = (TextView) findViewById(R.id.forget_psw_tv);

    }

    @Override
    protected void initListener() {
        mBtnEnsure.setOnClickListener(this);
        mForgetPswTv.setOnClickListener(this);
    }

    @Override
    protected void initData() {


    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_ensure:

                if(TextUtils.isEmpty(mEtOldPassword.getText())){
                    mEtOldPassword.setError("请输入旧密码");
                    return;
                }
                if(TextUtils.isEmpty(mEtNewPassword.getText())){
                    Toasty.success(this,mEtOldPassword.getText().toString().trim()).show();
                    mEtNewPassword.setError("请输入新的登录密码");
                    return;
                }
                if(TextUtils.isEmpty(mEtAgainNewPassword.getText())){
                    mEtAgainNewPassword.setError("请再次输入新的登录密码");
                    return;
                }
                if(!mEtNewPassword.getText().equals(mEtAgainNewPassword.getText())){
                    mEtAgainNewPassword.setError("两次输入的新密码不一致，请重新输入");
                }

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("oldPassword",mEtOldPassword.getText());
                jsonObject.put("newPassword",mEtNewPassword.getText());
                jsonObject.put("username", RxSPTool.getString(this,RxSPConfig.USER_PHONE));

                RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"), jsonObject.toString());

                alterLoginPwdPresenter.deliverData(body);

                break;
            case R.id.forget_psw_tv:
                SkipUtils.toForgetPassWord(this);
                break;
        }
    }

    @Override
    public void showSuccessMsg(String s, BaseBean addDABean) {
        Toasty.success(this,s).show();
    }

    @Override
    public void showErrorMsg(String s) {
        Toasty.error(this,s).show();
    }
}
